package com.eastfair.pay.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.pay.dto.ApplyRefundDTO;
import com.eastfair.pay.dto.ApplyRefundPageQuery;
import com.eastfair.pay.dto.ApplyRefundSaveDTO;
import com.eastfair.pay.dto.ApplyRefundUpdateDTO;
import com.eastfair.pay.dto.RefundDTO;
import com.eastfair.pay.entity.ApplyRefund;
import com.eastfair.pay.service.ApplyRefundService;
import com.eastfair.pay.vo.ApplyRefundVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 退款申请
 * </p>
 *
 * @author clm
 * @date 2022-06-08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/applyRefund")
@Api(value = "ApplyRefund", tags = "退款申请")
@PreAuth(replace = "pay:applyRefund:")
public class ApplyRefundController extends SuperController<ApplyRefundService, Long, ApplyRefund, ApplyRefundPageQuery, ApplyRefundSaveDTO, ApplyRefundUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<ApplyRefund> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ApplyRefund> applyRefundList = list.stream().map((map) -> {
            ApplyRefund applyRefund = ApplyRefund.builder().build();
            //TODO 请在这里完成转换
            return applyRefund;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(applyRefundList));
    }


    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ApplyRefundPageQuery> params) {
        ApplyRefundPageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 退款管理查询
     *
     * @return
     */
    @PostMapping("/queryList")
    @SysLog("退款管理查询")
    public R<Page<ApplyRefundVO>> queryList(@RequestBody @Validated PageParams<ApplyRefundPageQuery> params) {
        try {
            return R.success(baseService.queryPageList(params));
        } catch (BizException biz) {
            return R.fail(biz.getCode(), biz.getMessage());
        }

    }

    /**
     * 退款保存   仅限场馆预定用
     */

    @PostMapping("/saveApplyRefund")
    @SysLog("退款保存仅限场馆预定用")
    public R saveApplyRefund(@RequestBody ApplyRefundDTO applyRefundDTO) {

        try {
            baseService.saveApplyRefund(applyRefundDTO);
            return R.success();
        } catch (BizException biz) {
            return R.fail(biz.getCode(), biz.getMessage());
        }

    }

    /**
     * @return com.eastfair.core.base.R
     * @Author clm
     * @Description //接受退款申请
     * @Date 14:19 2023/2/20
     * @Param [applyRefundDTO]
     **/
    @PostMapping("/acceptApplyRefund")
    @SysLog("接受退款申请")
    public R acceptApplyRefund(@RequestBody ApplyRefundDTO applyRefundDTO) {

        try {
            baseService.acceptApplyRefund(applyRefundDTO);
            return R.success();
        } catch (BizException biz) {
            return R.fail(biz.getCode(), biz.getMessage());
        }

    }

    /**
     * @return com.eastfair.core.base.R
     * @Author clm
     * @Description //更新退款申请
     * @Date 14:37 2023/2/20
     * @Param [applyRefundDTO]
     **/
    @PostMapping("/updateApplyRefund")
    @SysLog("更新退款申请")
    public R updateApplyRefund(@RequestBody ApplyRefundDTO applyRefundDTO) {

        try {
            baseService.updateApplyRefund(applyRefundDTO);
            return R.success();
        } catch (BizException biz) {
            return R.fail(biz.getCode(), biz.getMessage());
        }

    }

    /**
     * 退款详情查询
     *
     * @param applyRefundId
     * @return
     */
    @GetMapping("applyRefundQueryById")
    @SysLog("退款详情查询")
    public R<ApplyRefundVO> applyRefundQueryById(@RequestParam("applyRefundId") @Validated @NotNull(message = "退款ID不能为为空") Long applyRefundId) {

        log.info("退款详情查询 入参 applyRefundId {} ", applyRefundId);
        try {
            //根据ID查询详情
            ApplyRefund applyRefund = baseService.getById(applyRefundId);
            //进行实体转化
            ApplyRefundVO applyRefundVO = new ApplyRefundVO();
            BeanUtils.copyProperties(applyRefund, applyRefundVO);
            applyRefundVO.setId(applyRefund.getId());
            return R.success(applyRefundVO);
        } catch (BizException biz) {
            return R.fail(biz.getCode(), biz.getMessage());
        }

    }


    /**
     * 退款详情查询
     *
     * @param orderNum
     * @return
     */
    @GetMapping("/queryApplyRefund/{orderNum}")
    @SysLog("退款详情查询")
    public R<ApplyRefundVO> applyRefundQueryByOrderNum(@PathVariable("orderNum") String orderNum) {

        log.info("退款详情查询 入参 applyRefundOrderNum {} ", orderNum);
        try {
            //根据ID查询详情
            ApplyRefund applyRefund = baseService.lambdaQuery().eq(ApplyRefund::getBusinessNum,orderNum).eq(ApplyRefund::getIsDeleted, BusinessConstant.DELETE_NO)
                    .eq(ApplyRefund::getIsEnabled,BusinessConstant.ENABLE_YES).one();
            //进行实体转化
            ApplyRefundVO applyRefundVO = new ApplyRefundVO();
            BeanUtils.copyProperties(applyRefund, applyRefundVO);
            applyRefundVO.setId(applyRefund.getId());
            return R.success(applyRefundVO);
        } catch (BizException biz) {
            return R.fail(biz.getCode(), biz.getMessage());
        }

    }

    /**
     * 确认退款
     */
    @PostMapping("/saveApplyRefundCnfirm")
    @SysLog("确认退款")
    public R saveApplyRefundCnfirm(@RequestBody ApplyRefundDTO applyRefundDTO) {

        try {
            baseService.saveApplyRefundCnfirm(applyRefundDTO);
            return R.success();
        } catch (BizException biz) {
            return R.fail(biz.getCode(), biz.getMessage());
        }

    }


    /**
     * @return com.eastfair.core.base.R<java.lang.Boolean>
     * @Author clm
     * @Description //审核退款申请
     * @Date 11:32 2023/2/14
     * @Param [invoice]
     **/
    @PostMapping("/examine")
    @SysLog("审核退款申请")
    public R<Boolean> examine(@RequestBody ApplyRefundDTO applyRefundDTO) {
        try {
            baseService.updateById(applyRefundDTO);
            return R.success();
        } catch (BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * @return com.eastfair.core.base.R<java.lang.Boolean>
     * @Author fwl
     * @Description //荆艺财务审核
     * @Date 11:32 2023/3/3
     * @Param [invoice]
     **/
    @PostMapping("/financialExamine")
    @SysLog("荆艺财务审核")
    public R financialExamine(@RequestBody ApplyRefundDTO applyRefundDTO) {
        try {
            baseService.financialExamine(applyRefundDTO);
            return R.success();
        } catch (BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * 银联在线退款
     *
     * @param dto
     * @return
     */
    @PostMapping("/online")
    public R<Boolean> onlineRefund(@RequestBody RefundDTO dto) {
        baseService.onlineRefund(dto);
        return R.success(true);
    }

    /**
     * @return com.eastfair.core.base.R<java.math.BigDecimal>
     * @Author clm
     * @Description //获取退款总金额
     * @Date 11:14 2023/2/24
     * @Param []
     **/
    @PostMapping("/getRefundTotalAmount")
    public R<BigDecimal> getRefundTotalAmount() {

        return R.success(baseService.getRefundTotalAmount());
    }

    /**
     * 通过业务主键集合查询发票申请状态集合
     *
     * @param buinessIds
     * @return
     */
    @PostMapping("/queryExamineState/buinessIds")
    public R<List<ApplyRefundVO>> queryRefundExamineState(@RequestBody List<Long> buinessIds) {
        try {
            return R.success(baseService.queryRefundExamineState(buinessIds));
        } catch (BizException biz) {
            return R.fail(biz);
        }

    }
}